package com.example.ntpsynchronoustime.utils;

import android.annotation.SuppressLint;

import androidx.annotation.Nullable;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by Jeremy on 2016/11/24.
 * 日期工具类
 */
@SuppressLint({"SimpleDateFormat"})
public class DateUtil {
    public static final String DATE_TIME_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取完整日期
     *
     * @param timeZone 默认为"GMT+8:00"
     * @return 如果时区没问题，则返回长度为4的数组，第一个为年，第二个为月，第三个为日，第四个为星期（1-7依次为周日到周六），否则返回null
     */
    public static int[] getFullDate(@Nullable String timeZone) {
        int[] date;
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeZone(TimeZone.getTimeZone(timeZone == null ? "GMT+8:00" : timeZone));
            date = new int[]{calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.DAY_OF_WEEK)};
        } catch (Exception e) {
            e.printStackTrace();
            date = null;
        }
        return date;
    }

    /**
     * 获取当前时间，格式为“HH:mm:ss”
     *
     * @return
     */
    public static String getTime() {
        return new SimpleDateFormat("HH:mm:ss", Locale.CHINA).format(new Date());
    }



    public static String getTimeInfo(long milSecond) {
        Date date = new Date(milSecond * 1000);
        SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
        return format.format(date);
    }

    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy/MM/dd  HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    public static long getActiveTimelong(String result) {
        SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = yearFormat.parse(result);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return System.currentTimeMillis();
    }

    /**
     * 获取当前时间字符串
     *
     * @return
     */
    public static String getNowTime() {
        String returnValue = "";
        SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_FULL);
        returnValue = df.format(new Date());
        return returnValue.replace(" ", "-");
    }

    public static String formatDate(Date date, String pattern, TimeZone timeZone) {
        DateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
        format.setTimeZone(timeZone);
        return format.format(date);
    }

    /*
     * 将时间戳转换为时间
     *
     * s就是时间戳
     */

    public static String stampToDate(String s, String format) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

}
